package caidan.net;

import caidan.res.Resource;
import jinyilw.common.app.AppConfig;
import jinyilw.common.database.JdbcTools;
import jinyilw.swing.FontTools;
import jinyilw.swing.rlaf.UITools;
import jinyilw.swing.visuals.Rcomp.RDialog;
import jinyilw.swing.visuals.comp.RichTextComp;
import jinyilw.tools.h2db.DBConfig;
import jinyilw.tools.net.NetTools;
import jinyilw.tools.net.ServerTools;
import jinyilw.tools.net.dev.BaseClient;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Window;

public class AdminNetFunDialog extends RDialog
{
	private static final long serialVersionUID = -4543942095886489476L;
	private JdbcTools jdbcTools;
	private boolean isConnected;
	private AdminSuggestPanel suggestPanel;
	private AdminNewFoodPanel newFoodPanel;
	private RichTextComp netSymbol;

	private ServerTools serverTools;
	private int clientNum = 0;
	private String hostIpAddress;

	public AdminNetFunDialog()
	{
		serverTools = ServerTools.getDefaultTools();
		init();
	}

	public AdminNetFunDialog(Frame owner, ServerTools server)
	{
		super(owner);
		serverTools = server;
		init();
	}

	public AdminNetFunDialog(Frame owner, boolean modal)
	{
		super(owner, modal);
		init();
	}

	public AdminNetFunDialog(Dialog owner, boolean modal)
	{
		super(owner, modal);
		init();
	}

	public AdminNetFunDialog(Window owner, String title)
	{
		super(owner, title);
		init();
	}

	public static void main(String[] args)
	{
		UITools.initRLookAndFeel();
		AdminNetFunDialog dialog = new AdminNetFunDialog();
		dialog.addClosingOperater((Object o) ->
		{
			System.exit(0);
		});
		dialog.setLocationRelativeTo(null);
		dialog.setVisible(true);
	}

	private void init()
	{
		createGUI();
		initLayout();
		initListeners();
		initNet();

		setWindowSymbol(netSymbol);
		setTitleHeight(38);
		setTitleFont(FontTools.getCustomFont(21));
		setTitleColor(Color.yellow);
		setTitle("网 络 功 能");
		setSection(8);
		setDefaultDimensionRatio(0.8, 0.85);
		defaultSize();
		AppConfig.addAppExitingOperator((Object o) ->
		{
			jdbcTools.releaseConnect();
			newFoodPanel.writeToFile();
		});
	}

	private void createGUI()
	{
		suggestPanel = new AdminSuggestPanel(serverTools);
		newFoodPanel = new AdminNewFoodPanel(serverTools);
		netSymbol = new RichTextComp();
		netSymbol.setImage(Resource.netIcon);
		netSymbol.setSize(400, 20);
		netSymbol.setFont(FontTools.getCustomFont(13));
		netSymbol.setHorizontalAlignment(SwingConstants.LEFT);
	}

	private void initLayout()
	{
		setLayout(new GridLayout(0, 2, 5, 5));
		add(suggestPanel);
		add(newFoodPanel);
	}

	private void initListeners()
	{
		netSymbol.addClickOperator((Object o) ->
		{
			if (clientNum < 1 || !serverTools.isConnected())
				return;
			BaseClient[] clients = serverTools.getConnectedClients();
			JPopupMenu popup = new JPopupMenu();
			JMenuItem item;
			for (BaseClient client : clients)
			{
				Object info = client.getInfo();
				if (info == null)
					info = client.getRemoteDetail();
				item = new JMenuItem("已连接" + info);
				item.setFont(FontTools.defaultFont);
				popup.add(item);
			}
			popup.show(netSymbol, 20, netSymbol.getHeight());
		});
	}

	private void initNet()
	{
		jdbcTools = DBConfig.getDefaultTools();
		isConnected = jdbcTools.isConnected();

		hostIpAddress = NetTools.getHostIP();
		if (isConnected)
		{
			initClientNum();
			netSymbol.setForeground(Color.green.darker());
		} else
		{
			netSymbol.setText("未连接(管理员)");
			netSymbol.setForeground(Color.red);
		}

		if (DBConfig.needInitial())
		{
			jdbcTools.executeSql(
					"create table adminSuggest(id int(11) not null primary key auto_increment,"
							+ "user varchar,content varchar,dateString varchar)");
			boolean ok = jdbcTools.executeSql(
					"create table suggest(id int(11) not null primary key auto_increment,"
							+ "user varchar,content varchar,dateString varchar)");
			if (ok)
				DBConfig.setNeedInitial(false);
		}
		serverTools.addClientConnectingOperator((BaseClient c) ->
		{
			updateClientNum();
		});
		serverTools.addClientDisconnectOperator((BaseClient c) ->
		{
			updateClientNum();
		});
		suggestPanel.initNet();
		newFoodPanel.initNet();
	}

	@Override
	public void setVisible(boolean b)
	{
		super.setVisible(b);
		if (b)
			new Thread(() ->
			{
				if (!jdbcTools.isConnected())
					jdbcTools.reConnect();
			}).start();
	}

	private void initClientNum()
	{
		clientNum = 0;
		if (serverTools != null)
			clientNum = serverTools.getAllClientsNumber();
		netSymbol.setText(
				"已连接(管理员)  本机IP<" + hostIpAddress + ">    在线数：" + clientNum);
	}
	private void updateClientNum()
	{
		if (serverTools != null)
		{
			int num = serverTools.getAllClientsNumber();
			if (clientNum != num)
				netSymbol.setText(
						"已连接(管理员)  本机IP<" + hostIpAddress + ">    在线数：" + num);
			clientNum = num;
		}
	}
}
